<?php
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
function GetSql($table,$cols,$order,$desc,$offset,$row)
{
	if(!@$cols)
	{
		//return ;
	}
	if(!is_array($cols)&&@$cols)
	{
		$query = "SELECT * FROM {$table} WHERE id={$cols}";
	}
	else
	{
		$query = "SELECT * FROM {$table} WHERE 1=1";
		if (@$cols)
		{
			foreach($cols as $col => $value)
			{
				$query .= " and {$col}='{$value}'";
			}
		}
	}
	if(@$order&&$order!='id')
	{
		$query .= " order by {$order}";
	}
	if($desc)
	{
		$query .= " desc";
	}
	if($offset>0)
	{
		$query .= " Limit ".$offset;
		if($row>0)
		{
			$query .= ", ".$row;
		}
		else
		{
			$query .= ", -1";
		}
	}
	else if($row>0)
	{
		$query .= " Limit ".$row;
	}
	return $query;
}

function GetWithLikeSql($table,$cols,$like_cols,$order,$desc,$offset,$row)
{
	if(!@$cols)
	{
		//return ;
	}
	if(!is_array($cols)&&@$cols)
	{
		$query = "SELECT * FROM {$table} WHERE id={$cols}";
	}
	else
	{
		$query = "SELECT * FROM {$table} WHERE 1=1";
		if (@$cols)
		{
			foreach($cols as $col => $value)
			{
				$query .= " and {$col}='{$value}'";
			}
		}
	}
	foreach($like_cols as $like_col => $value)
	{
		$query .= " and {$like_col} Like '%{$value}%'";
	}
	if(@$order&&$order!='id')
	{
		$query .= " order by {$order}";
	}
	if($desc)
	{
		$query .= " desc";
	}
	if($offset>0)
	{
		$query .= " Limit ".$offset;
		if($row>0)
		{
			$query .= ", ".$row;
		}
		else
		{
			$query .= ", -1";
		}
	}
	else if($row>0)
	{
		$query .= " Limit ".$row;
	}
	return $query;
}

function InsSql($table,$cols)
{
	if(!@$cols)
	{
		return ;
	}
	if(!is_array($cols))
	{
		$query = "INSERT INTO {$table} (id) VALUES ('{$cols}')";
	}
	else
	{
		$query = "INSERT INTO {$table} ";

		$key = array();
		$val = array();
		foreach($cols as $col => $value)
		{
			array_push($key,$col);
			array_push($val,$value);
		}

		$colstr = "(";
		$valstr = "(";
		for($i=0;$i<sizeof($cols);)
		{
			$colstr .= "`{$key[$i]}`";
			$valstr .= "'{$val[$i]}'";
			if(++$i!=sizeof($cols))
			{
				$colstr .= ", ";
				$valstr .= ", ";
			}
		}
		$colstr .=") ";
		$valstr .=") ";

		$query = $query.$colstr."VALUES ".$valstr;
	}
	return $query;
}
function SetSql($table,$src_cols,$dest_cols)
{
	if(!@$src_cols)
	{
		return ;
	}
	if(!@$dest_cols)
	{
		return ;
	}
	if(!is_array($dest_cols))
	{
		return ;
	}
	$query = "UPDATE {$table} SET ";
	foreach($dest_cols as $col => $value)
	{
		if($col=="Default")
		{
			$col = "[".$col."]";
		}
		$query .= " {$col}='{$value}',";
	}
	$query[strlen($query)-1]=" ";
	if(!is_array($src_cols))
	{
		$query .= "WHERE id={$src_cols}";
	}
	else
	{
		$query .= "WHERE 1=1";
		foreach($src_cols as $col => $value)
		{
			if($col=="Default")
			{
				$col = "[".$col."]";
			}
			$query .= " and {$col}='{$value}'";
		}
	}
	return $query;
}
?>